#include "delay.h"
#include "key.h"

void Key_Init(void){
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE ,ENABLE);
    GPIO_InitStructure.GPIO_Pin = Key0_Pin|Key1_Pin|Key2_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设置成下拉输入，按键按下时变为低电平
    GPIO_Init(Key0_Port ,&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = WK_UP_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入按键按下变为高电平
    GPIO_Init(WK_UP_Port ,&GPIO_InitStructure);
}

u8 Key_Scan(u8 model){ 
    static u8 Key_Up = 0; //按键松开标志
    if (model)
    {
        Key_Up = 1; //支持连续按压
    }

    if(Key0_Value == 0|| Key1_Value == 0 || Key2_Value == 0 || WK_UP_Value ==1){ //key0,1,2按下时接地读出低电平，wk_up为高电平

        Key_Up = 1;
       if (Key0_Value == 1)
       {
        return Key0;
       }
       if (Key1_Value == 1)
       {
        return Key1;
       }
       if (Key2_Value == 1)
       {
        return Key2;
       }
       if (WK_UP_Value == 1)
       {
        return WK_UP;
       }   
    }else if (!Key_Up)
    {
        return Key_Up;
    }
}